package ppjava12v1.chess.entities.users;

import ppjava12v1.chess.enums.Role;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "usr_roles")
public class UserRole implements Serializable {

    @ManyToOne(targetEntity = User.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id", nullable = false)
    @Id
    private User user;

    @Enumerated(EnumType.STRING)
    @Id
    private Role role;

    @Column
    private String login;

    public UserRole(User user, Role role) {
        this.user = user;
        this.role = role;
        this.login = user.getLogin();
    }

    public UserRole() {
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof UserRole)) return false;

        UserRole userRole = (UserRole) o;

        if (role != userRole.role) return false;
        if (!user.equals(userRole.user)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = user.hashCode();
        result = 31 * result + role.hashCode();
        return result;
    }


}
